package edu.pku.sei.transformation.editor.model.base;

import java.util.ArrayList;
import java.util.Collection;

public class NameSpace extends NamedElement implements INameSpace {
	/**
	 * for now, this member can't be saved, so every time, this element is
	 * loaded from file, this member should be create and register elements in
	 * it.
	 */
	protected INameSpace nameTable = null;
	/**
	 * whether distinguish elements by element name if this property is true, if
	 * two elements have same superNamespace and same name, we think this two
	 * element are same else, if two elements are two references of one same
	 * object, we think this two elements are same.
	 */
	protected boolean distinguishByName = false;
	private static ENameTableType defaultTableType = ENameTableType.none;

	public NameSpace() {
		this(defaultTableType);
	}

	/**
	 * initialize NameSpace with INameTable of tableType
	 * 
	 * @param tableType
	 */
	public NameSpace(ENameTableType tableType) {
		if (tableType == ENameTableType.hashTable)
			nameTable = new HashNameTable();
		else if (tableType == ENameTableType.none)
			nameTable = null;
		else
			nameTable = null;
	}

	public Collection<NamedElement> getAllElements() {
		if (nameTable != null)
			return nameTable.getAllElements();
		else
			return new ArrayList<NamedElement>();
	}

	public NamedElement getElement(String name) {
		if (nameTable != null)
			return nameTable.getElement(name);
		else
			return null;
	}

	public boolean registerName(NamedElement element) {
		if (nameTable != null)
			return nameTable.registerName(element);
		else
			return false;
	}

	public boolean unregisterName(NamedElement element) {
		if (nameTable != null)
			return nameTable.unregisterName(element);
		else
			return false;
	}

	public boolean unregisterName(String name) {
		if (nameTable != null)
			return nameTable.unregisterName(name);
		else
			return false;
	}

}
